package cn.edu.zafu.bigdata.lekang.shiyan5;

class InvalidAgeException extends Exception {
    public InvalidAgeException(String message) {
        super(message);
    }
}

public class AgeValidation {

    public static void main(String[] args) {
        // 测试数据
        int age1 = 20;
        int age2 = 17;

        try {
            checkAge(age1);
            System.out.println("年龄 " + age1 + " 是有效的");
        } catch (InvalidAgeException e) {
            System.out.println("错误: " + e.getMessage());
        }

        try {
            checkAge(age2);
            System.out.println("年龄 " + age2 + " 是有效的");
        } catch (InvalidAgeException e) {
            System.out.println("错误: " + e.getMessage());
        }
    }

    public static void checkAge(int age) throws InvalidAgeException {
        if (age < 18) {
            throw new InvalidAgeException("年龄 " + age + " 无效，必须大于或等于18岁");
        }
    }
}
